// DAO/Impl/AssignmentGradeDaoImpl.java
package com.hp.DAO.Impl;                         // 实现包

import com.hp.DAO.AssignmentGradeDao;            // 接口：AssignmentGradeDao
import com.hp.DAO.BaseDao;                       // 基类
import com.hp.Model.AssignmentGrade;             // 实体：作业评分
import com.hp.Util.RowMapper;                    // 行映射器
import com.hp.Util.SimpleJdbcTemplate;           // JDBC 模板

import java.sql.Timestamp;                // 时间戳

/** AssignmentGradeDao 的 JDBC 实现 */
public class AssignmentGradeDaoImpl extends BaseDao implements AssignmentGradeDao {
    public AssignmentGradeDaoImpl(SimpleJdbcTemplate jdbc) { super(jdbc); } // 构造注入

    // RowMapper：ResultSet -> AssignmentGrade
    private static final RowMapper<AssignmentGrade> M = rs -> {
        AssignmentGrade g = new AssignmentGrade();                           // 新建实体
        g.setId(rs.getLong("id"));                                           // id
        g.setSubmissionId(rs.getLong("submission_id"));                      // 提交ID
        g.setGradedBy(rs.getLong("graded_by"));                              // 评分人
        g.setScore(rs.getInt("score"));                                      // 分数
        g.setFeedback(rs.getString("feedback"));                              // 评语
        Timestamp gt = rs.getTimestamp("graded_at");                          // 评分时间
        g.setGradedAt(gt == null ? null : new java.util.Date(gt.getTime())); // 转 Date
        return g;                                                             // 返回
    };

    @Override
    public AssignmentGrade findBySubmissionId(Long submissionId) {            // 按提交ID查询
        String sql = "SELECT id,submission_id,graded_by,score,feedback,graded_at FROM assignment_grade WHERE submission_id=?"; // SQL
        return queryOne(sql, M, submissionId);                                // 执行
    }

    @Override
    public Long insert(AssignmentGrade g) {                                   // 新增评分
        String sql = "INSERT INTO assignment_grade (submission_id,graded_by,score,feedback,graded_at) VALUES (?,?,?,?,?)"; // SQL
        Timestamp graded = g.getGradedAt() == null ? new Timestamp(System.currentTimeMillis())
                : new Timestamp(g.getGradedAt().getTime()); // 评分时间
        return execInsertReturnId(sql, g.getSubmissionId(), g.getGradedBy(), g.getScore(), g.getFeedback(), graded); // 执行插入
    }

    @Override
    public int updateScoreAndFeedback(AssignmentGrade g) {                    // 更新分数与评语
        String sql = "UPDATE assignment_grade SET score=?, feedback=?, graded_at=? WHERE submission_id=?"; // SQL
        Timestamp graded = g.getGradedAt() == null ? new Timestamp(System.currentTimeMillis())
                : new Timestamp(g.getGradedAt().getTime()); // 评分时间
        return execUpdate(sql, g.getScore(), g.getFeedback(), graded, g.getSubmissionId()); // 执行更新
    }

    @Override
    public int deleteBySubmissionId(Long submissionId) {                      // 删除评分
        String sql = "DELETE FROM assignment_grade WHERE submission_id=?";    // SQL
        return execUpdate(sql, submissionId);                                 // 执行删除
    }
}
